﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;
using System.IO;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.User.Identity.IsAuthenticated)
            Response.Redirect("login.aspx");



        if (!Page.IsPostBack)
        {
            
            string strsql = "SELECT count(*) as counter FROM class";
            SqlDataReader sdr = SQLHelper.SQLHelper.ExecReader(strsql);
            if (sdr.Read())
            {
                int count_class = (int)sdr[0];

                if (count_class==0)
                {
                    DropDownListClass.Visible = Button5.Visible = false;
                    DropDownList1.Visible = DropDownList2.Visible = TextBoxClass.Visible = true;
                }

            }


            int i;
            ArrayList alYear = new ArrayList();
            ArrayList alMonth = new ArrayList();
            ArrayList alDay = new ArrayList();

            for (i = DateTime.Now.Year; i > 1900; i--)
                alYear.Add(i.ToString());

            for (i = 1; i <= 12; i++)
                alMonth.Add(i.ToString());

            for (i = 1; i < 32; i++)
                alDay.Add(i.ToString());

            ddlYear.DataSource = alYear;
            ddlYear.DataBind();

            ddlMonth.DataSource = alMonth;
            ddlMonth.DataBind();

            ddlDay.DataSource = alDay;
            ddlDay.DataBind();

        }
        
        string select_class=Request.QueryString["class"];
        if (Roles.IsUserInRole("班级管理员") && (!Roles.IsUserInRole("超级管理员")))
        {
            SqlDataSource1.SelectCommand = string.Format("SELECT * FROM [member] where class in (select [class] from class where owner='"+ User.Identity.Name  + "')");
        }
        else if (select_class != null)
        {
            SqlDataSource1.SelectCommand = string.Format("SELECT * FROM [member] where class='" + select_class + "'");
        }



    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        Button1.Visible = ListView1.Visible=false;        
        Panel1.Visible = true;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        DropDownList1.Visible = DropDownList2.Visible = TextBoxClass.Visible = Button7.Visible = true;
        DropDownListClass.Visible=Button5.Visible = false;

        
        
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        DropDownList1.Visible = DropDownList2.Visible = TextBoxClass.Visible = Button7.Visible = false;
        DropDownListClass.Visible=Button5.Visible = true;
    }

    protected void Button4_Click(object sender, EventArgs e)
    {

        string _user = Page.User.Identity.Name;
        string _name = TextBoxName.Text;
        string _sex = DropDownListSex.Text;
        string _birth = new DateTime(int.Parse(ddlYear.Text), int.Parse(ddlMonth.Text), int.Parse(ddlDay.Text)).ToLongDateString();
        string _image = DateTime.Now.ToFileTime().ToString();
        string _imageurl = "upload/" + _image;
        
        string _address = TextBoxAddress.Text;
        string _phone = TextBoxPhone.Text;
        string _email = TextBoxEmail.Text;
        string _class=DropDownListClass.Text;
        if (!DropDownListClass.Visible)
        {
            _class = TextBoxClass.Text;
        }

        if (_class == "")
        {
            Label1.Text = "班级不能为空!";
            return;
        }

        //创建班级
        if (DropDownListClass.Visible == false)
        {
            {
                string strsql = "SELECT * FROM CLASS WHERE department='" + DropDownList1.Text + "' AND grade='" + DropDownList2.Text + "' AND class='" + _class + "'";
                SqlDataReader sdr = SQLHelper.SQLHelper.ExecReader(strsql);
                if (sdr.Read())
                {
                    Label1.Text = "该班级已存在!";
                    return;
                }
            }


            try
            {

                string strsql = "insert into class (department,grade,class,created,owner) values ('" + DropDownList1.Text + "','" + DropDownList2.Text + "','" + _class + "','" + DateTime.Now + "','" + Page.User.Identity.Name + "')";
                SQLHelper.SQLHelper.ExecNonQuery(strsql);

            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
                return;
            }
        }
        

        //上传头像
        Boolean fileOK = false;
        String path = Server.MapPath("upload/");//可考虑用server.path来存储相对地址
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        if (FileUpload1.HasFile)
        {
            String fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(path
                    + _image);
            }
            catch
            {
                Label1.Text = "头像上传失败!";
                return;
            }
        }

        //添加联系人
        try
        {
            string strsql = "insert into member ([user],[name],sex,birth,address,phone,email,class,image) values ('" + _user + "','" + _name + "','" + _sex + "','" + _birth + "','" + _address + "','" + _phone + "','" + _phone + "','" + _class + "','" + _imageurl + "')";
            SQLHelper.SQLHelper.ExecNonQuery(strsql);
        }
        catch
        {
            Label1.Text = "添加联系人失败!";
            return;
        }



        Response.Redirect(Request.Url.ToString());
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString()); 
    }
}